import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import basic_func.general_func as bf

def plot_group_bars(df,params:dict):
    bf.zh_font(params)
    x = np.arange(len(df.index))
    colors = bf.color_selector(df,params)
    width = params['width']
    hatchs = params['hatchs'].split(sep=',')
    fig,ax = plt.subplots()
    for i,colname in enumerate(df):
        rects = ax.bar(x+width*i,df[colname],width,
                       color = colors[i],
                       edgecolor=params['edge_color'],
                       lw = params['line_width'],
                       hatch = hatchs[i],
                       label = df.columns[i])
        if params['show_number'] == 'yes':
            ax.bar_label(rects,padding=params['padding'])

    bf.title_and_labels(params)
    ax.set_xticks(x + width, df.index)
    ax.legend(loc='upper left', ncols=3)
    ax.set_ylim(bf.get_tuple(params['ylim']))
    plt.show()
    

if __name__=='__main__':
    df,params = bf.read_data('test_data/分组柱状图.xlsx')
    plot_group_bars(df,params)


